package com.scenery.server.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.scenery.server.services.UserServices;
import com.scenery.server.util.Log;
import com.scenery.server.vo.User;

/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String tag=UserServlet.class.getName();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("UserServlet");
		String action=request.getParameter("action");
		String userid=request.getParameter("userid");
		String password=request.getParameter("password");
		UserServices services=new UserServices();
		
		
		PrintWriter out=response.getWriter();
		if(action.equals("login"))
		{
			int ret=0;
			ret=services.login(userid, password);
			out.println(ret);
			out.close();
		}else if(action.equals("regist"))
		{
			int ret=0;
			ret=services.insert(userid, password);
			out.println(ret);
			out.close();
		}else if(action.equals("location"))
		{
			Log.d(tag, "用户坐标：=》");
			User u=new User();
			u.setUserId(userid);
			u.setLon(request.getParameter("lon"));
			u.setLat(request.getParameter("lat"));
			Log.d(tag, "用户坐标lon：=》lon"+u.getLon()+",lat"+u.getLat());
			
			int i=services.isFirst(userid);
			if(i==0)
			{
				Log.d(tag, "first");
				i=services.addUserLocation(u);
			}else if(i==1)
			{
				Log.d(tag, "no first");
				i=services.updateUserLocation(u);
			}
			out.println(i);
			out.close();
		}
		else if(action.equals("friend"))
		{
			Log.d(tag, "坐标");
			JSONObject jo=new JSONObject();
			List<User> list=new ArrayList<User>();
			list=services.getOther(userid);
			try {
				jo.put("code", "100");
				jo.put("msg", "成功");
				jo.put("list", list);
				Log.d(tag, "坐标数据："+jo.toString());
				out.println(jo.toString());
				out.close();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
